$(function() {
	if(activeType == "4") {
		setBuyProNum();
	}
});

/**
 * 审核通过
 * @returns {Boolean}
 */
function auditPass() {
	var remark = $.trim($("#remark").val());
	if(remark == "") {
		layer.alert("请输入审核意见！");
		return false;
	}
	layer.confirm("确定审核通过？", function() {
		layer.closeAll("dialog");
		var loadLayerIndex = layer.load(2); 
		$.ajax({
			type: "post",
			url: path + "/presentActivityController/auditPass",
			data: {
				id: activeId,
				remark: remark
			},
		    dataType: "text",
			success: function(data) {
				if (data == "success") {
					layer.msg("操作成功！", {icon: 1, time: 500}, function() {
						var index = parent.layer.getFrameIndex(window.name);
						parent.search();
						parent.layer.close(index);
					});
				} else {
		   			layer.alert(data);
		   		}
			},
			error: function() {
				layer.alert("操作失败！");
			},
			complete: function(XMLHttpRequest, textStatus) {
				layer.close(loadLayerIndex);
			}
		});
	});
}

/**
 * 审核驳回
 * @returns {Boolean}
 */
function auditBack() {
	var remark = $.trim($("#remark").val());
	if(remark == "") {
		layer.alert("请输入审核意见！");
		return false;
	}
	layer.confirm("确定审核驳回？", function() {
		layer.closeAll("dialog");
		var loadLayerIndex = layer.load(2);
		$.ajax({
			type: "post",
			url: path + "/presentActivityController/auditBack",
			data: {
				id: activeId,
				remark: remark
			},
		    dataType: "text",
			success: function(data) {
				if (data == "success") {
					layer.msg("操作成功！", {icon: 1, time: 500}, function() {
						var index = parent.layer.getFrameIndex(window.name);
						parent.search();
						parent.layer.close(index);
					});
				} else {
		   			layer.alert(data);
		   		}
			},
			error: function(data) {
				layer.alert("操作失败！");
			},
			complete: function(XMLHttpRequest, textStatus) {
				layer.close(loadLayerIndex);
			}
		});
	});
}

/**
 * 买赠活动，计算参与活动的商品个数
 */
function setBuyProNum() {
	var idArr = [];
	// 查找以"buyProTd_id_"开头的TD
	$("#buyProTbody").find("td[id^='buyProTd_id_']").each(function() {
		var id = $(this).text();
		if(!isExists(idArr, id)) {
			idArr.push(id);
		}
	});
	$("#buyProNum").text(idArr.length);
}

/**
 * 根据值，查找数组中是否存在
 * @param arr
 * @param v
 * @returns {Boolean} true存在，false不存在
 */
function isExists(arr, v) {
	var flag = false;
	for(var i in arr) {
		if(arr[i] == v) {
			flag = true;
			break;
		}
	}
	return flag;
}